    var video = document.querySelector('video');
    var text = document.getElementById('text');
    var upload_button = document.getElementById('upload_button')
    var preview_button = document.getElementById('preview_button')
    // var canvas1 = document.getElementById('qr-canvas');
    // var context1 = canvas1.getContext('2d');
    var mediaStreamTrack;
   console.log(navigator)
    // 一堆兼容代码
    window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL);
    if (navigator.mediaDevices === undefined) {
    navigator.mediaDevices = {};
    }
    if (navigator.mediaDevices.getUserMedia === undefined) {
    navigator.mediaDevices.getUserMedia = function(constraints) {
     var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
     if (!getUserMedia) {
     return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
     }
     return new Promise(function(resolve, reject) {
     getUserMedia.call(navigator, constraints, resolve, reject);
     });
    }
    } 
   
    //摄像头调用配置
    var mediaOpts = {
    audio: false,
    video: true,
    video: { facingMode: "environment"}, // 或者 "user"
    video: { width: 400, height: 400 }
    // video: { facingMode: { exact: "environment" } }// 或者 "user"
    }


    // 定义摄像头启用成功后的操作
    function successFunc(stream) {
    mediaStreamTrack = stream;
    video = document.querySelector('video');
    if ("srcObject" in video) {
     video.srcObject = stream
    } else {
     video.src = window.URL && window.URL.createObjectURL(stream) || stream
    }
    video.play();
    }
    // 
    function errorFunc(err) {
    alert(err.name);
    }


    // 启动摄像头函数
    function recordingVideo(stream){
        var videorecorder = new MediaRecorder(stream);
        var recorderControl=document.querySelector("#recorderControl"); 
        
        // 调用屏幕显示
        successFunc(stream);
        
        // 建立回调函数，点击录制按钮后的相应
        var buffers=[];
        recorderControl.onclick=function(){
        // this.textContent=="录制"?video.play():video.pause();
        if(this.textContent=="开始录制"){
            var video = document.getElementById('video');
            video.srcObject = stream;
            video.play();
            videorecorder.start();
        }else{
            videorecorder.stop();
        }
        this.textContent=this.textContent=="开始录制"?"停止":"开始录制";
        };

        videorecorder.ondataavailable=function(event){
            // console.log("aaa")
            buffers=event.data;
            var file = new File([buffers], 'video.mkv');
            var formdata = new FormData();
            var csrf_data = $('[name=csrfmiddlewaretoken]').val();
            formdata.append("file_obj", file);
            formdata.append('csrfmiddlewaretoken',csrf_data);
            var upload_button = document.getElementById('upload_button');
            var preview_button = document.getElementById('preview_button');
            upload_button.disabled = false;
            preview_button.disabled = false;
            set_upload(formdata);
            preview_button.onclick = function(){
                video = document.querySelector('video');
                // src_url = buffers;
                // console.log(buffers);
                // url = window.URL.createObjectURL(buffers);
                // console.log(url);
                // if ("srcObject" in video) {
                //     video.srcObject = buffers;
                // } else {
                //     video.src = window.URL && window.URL.createObjectURL(buffers) || buffers;
                // }
                    video.style = "";
                    video.srcObject = null;
                    video.src=URL.createObjectURL(buffers)
                    video.play();
                }

            }
        }

    // 启动文件上传功能
    function selectFile() {
    var for_in = document.getElementById('file_upload');
    if(for_in != null)
        document.body.removeChild(for_in);
    var inputObj = document.createElement('input');
    　　inputObj.setAttribute('id', 'file_upload');
    　　inputObj.setAttribute('type', 'file');
    　　inputObj.setAttribute("style", 'visibility:hidden');
    　　document.body.appendChild(inputObj);
　　inputObj.click();
    upload_button.disabled = false;
    preview_button.disabled = false;
    // var form_data = new FormData();
    // do{
    //     var file_info =$('#file_upload')[0].files[0];
    //     var csrf_data = $('[name=csrfmiddlewaretoken]').val();
    // }
    // while(typeof(file_info) != undefined);
    // console.log(file_info);
    // form_data.append('file_obj',file_info);
    // form_data.append('csrfmiddlewaretoken',csrf_data);
    // set_upload(form_data);
    // closeMedia();
        preview_button.onclick = function(){
            video = document.querySelector('video');
            video.style = "";
            video.srcObject = null;
            video.src=URL.createObjectURL($('#file_upload')[0].files[0])
            console.log($('#file_upload')[0].files);
            video.play();
            preview_button.disabled = true;
        }
        upload_button.onclick = function(){
            var inputObj = document.getElementById('file_upload')
            file = inputObj.files[0];
            var formdata = new FormData();
            var csrf_data = $('[name=csrfmiddlewaretoken]').val();
            formdata.append("file_obj", file);
            formdata.append('csrfmiddlewaretoken',csrf_data);
            set_upload(formdata);
        }
    } 


    // 正式启动摄像头
    function openMedia(){

    navigator.mediaDevices.getUserMedia(mediaOpts).then(recordingVideo);
    video.setAttribute("style","");
    // .catch(errorFunc);
    var recording_btn = document.getElementById('recorderControl');
    recording_btn.disabled = false;
    document.getElementById('close_btn').disabled = false;
    document.getElementById('open_btn').disabled = true;
    }
   
    //关闭摄像头
    function closeMedia(){
    mediaStreamTrack.getVideoTracks().forEach(function (track) {
    track.stop();
    //  context1.clearRect(0, 0,context1.width, context1.height);//清除画布
    });
    disable_btn();
    video.setAttribute("style","display:none");
    }

    function disable_btn(){
        btn_id = ['close_btn', 'recorderControl'];
        btn_id.forEach(function(id_str){
            var btn = document.getElementById(id_str);
            btn.disabled = true;
        });
        document.getElementById('open_btn').disabled = false;
    }

    // 设置点击上传的动作
    function set_upload(formdata){
        var upload_button = document.getElementById('upload_button');
        upload_button.onclick = function(){
            $.ajax(
            {
                // url填写django对应的路由
                url:'/content_update/',
                type:'post',
                data:formdata,
                processData: false,
                contentType: false,
                beforeSend: function(xhr) {
                    // console.log("assfa")
                    var loading = '<div class="loader">'
                    + '<div class="face">'
                    + '<div class="circle"></div>'
                    + '</div>'
                    + '<div class="face">'
                    + '<div class="circle"></div>'
                    + '</div>'
                    + '</div>';
                    // var loading = document.createElement()
                    var result_col = document.getElementById('result_col');
                    result_col.innerHTML = loading;
                },
                success: function(response) {
                    console.log(response)
                    result_tag = document.getElementById('result_col');
                    result_inserted = '<div class="center-vertical" id="result_tag">' + response.result + '</div>'
                                        + '<div class="row" id="tag_info">'
                                        + '<div class="col-md-6" id="img_path"><img src='+response.img_path+' style="width: 256px;height:192px"></div>' //这里放入相关图片
                                        + '<div class="col-md-6" id="baike_content"><p style="text-align:left;">'+response.baike_content+'</p></div>' //这里放入相关文本
                                        + '</div>'
                                        + '<div id="related_title">相关链接</div>' 
                                        + '<div class="row" style="overflow-x:auto; overflow-y:auto" id="relate_link">'
                    for(var i=0;i<response.relate_link.length;i++){
                        console.log(response.relate_link[i])
                        result_inserted += "<div id='related_url'>"+ '<a id="related_url" target="_blank" href=' + response.relate_link[i] + '><h5>'+response.relate_link_title[i]+'</h5></a>'
                         + response.relate_link_abstract[i] + "</div>"//这里放入相关链接
                    }
                    result_inserted += '</div>'
                    // result_inserted = '<div class="center-vertical">' + response.result + '</div>'
                    result_tag.innerHTML = result_inserted;
                    // console.log(response);
                },
                error: function(info) {
                    console.log('error', info);
                }
            }
            )
        };
    }